//
//  MyGetMoneyViewController.m
//  VEMoneyPorject
//
//  Created by 陈建 on 2018/9/25.
//  Copyright © 2018年 陈建. All rights reserved.
//

#import "MyGetMoneyViewController.h"
#import "SPPageMenu.h"
#import "MyGetMoneyChildsViewController.h"
#define kPageMenuH 40
@interface MyGetMoneyViewController ()<SPPageMenuDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) SPPageMenu *pageMenu;

@end

@implementation MyGetMoneyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kWhiteColor;
    
    [self addChildViewController:[[MyGetMoneyChildsViewController alloc] init]];
    [self addChildViewController:[[MyGetMoneyChildsViewController alloc] init]];
    
    [self.view addSubview:self.scrollView];
    [self.view addSubview:self.pageMenu];
    // 先将第一个子控制的view添加到scrollView上去
    [self.scrollView addSubview:self.childViewControllers[0].view];
    // Do any additional setup after loading the view.
}
- (SPPageMenu *)pageMenu {
    
    if (!_pageMenu) {
        _pageMenu = [SPPageMenu pageMenuWithFrame:CGRectMake(0, 0, kScreenWidth, kPageMenuH) trackerStyle:SPPageMenuTrackerStyleLineAttachment];
        [_pageMenu setItems:@[@"全部收益",@"邀请收益"] selectedItemIndex:0];
        _pageMenu.delegate = self;
        _pageMenu.itemTitleFont = [UIFont systemFontOfSize:14];
        _pageMenu.selectedItemTitleColor =  kCommonRedColor;
        _pageMenu.unSelectedItemTitleColor = [UIColor colorWithHexString:kDetailTextBlackColor];
        _pageMenu.tracker.backgroundColor =kCommonRedColor;
        _pageMenu.permutationWay = SPPageMenuPermutationWayNotScrollEqualWidths;
        _pageMenu.bridgeScrollView = self.scrollView;
        
    }
    return _pageMenu;
}

- (void)pageMenu:(SPPageMenu *)pageMenu itemSelectedFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    if (!self.childViewControllers.count) { return;}
    // 如果上一次点击的button下标与当前点击的buton下标之差大于等于2,说明跨界面移动了,此时不动画.
    if (labs(toIndex - fromIndex) >= 2) {
        [self.scrollView setContentOffset:CGPointMake(_scrollView.frame.size.width * toIndex, 0) animated:NO];
    } else {
        [self.scrollView setContentOffset:CGPointMake(_scrollView.frame.size.width * toIndex, 0) animated:YES];
    }
    MyGetMoneyChildsViewController *targetViewController = self.childViewControllers[toIndex];
    // 如果已经加载过，就不再加载
    if ([targetViewController isViewLoaded]) return;
    targetViewController.currentIndex = toIndex;
    targetViewController.view.frame = CGRectMake(kScreenWidth*toIndex, 0, kScreenWidth, self.scrollView.height);
    
    [self.scrollView addSubview:targetViewController.view];
    
}


- (UIScrollView *)scrollView {
    
    if (!_scrollView) {
        
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.frame = CGRectMake(0, kPageMenuH, kScreenWidth, kScreenHeight-Height_TabBar_Space-Height_NavBar - kPageMenuH);
        
        _scrollView.pagingEnabled = YES;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.contentSize = CGSizeMake(kScreenWidth*2, 0);
        _scrollView.backgroundColor = [UIColor colorWithWhite:1 alpha:0];
    }
    return _scrollView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
